#include "ext_uart.h"
#include "../utility.h"

static TaskHandle_t xPrvExtUartRxTask;
static TaskHandle_t xBlockExtUartTxTask;
static QueueHandle_t xExtUartTxBlockQueue = NULL;

static void prvExtUartRxTask( void *pvParameters )
{
    QueueHandle_t xEventQueue = (QueueHandle_t)pvParameters;

    signed char cRxedChar;
    static char cInputString[ 1024 ];
    u16 ucInputIndex = 0;
    xComPortHandle xPort;

	for( ;; )
	{
		/* Wait for the next character.  The while loop is used in case
		INCLUDE_vTaskSuspend is not set to 1 - in which case portMAX_DELAY will
		be a genuine block time rather than an infinite block time. */
		while( xExtUartGetChar( xPort, &cRxedChar, portMAX_DELAY ) != pdPASS );
        cInputString[ucInputIndex] = cRxedChar;
        ucInputIndex = ucInputIndex + 1;
        if(ucInputIndex == 10){
            Event event;
            u8 *pData = (u8*)pvPortMalloc(sizeof(u8) * 10);
            memcpy(pData,cInputString,10);
            makeEvent(&event, 10, EVENT_EXT_UART_RX_OK, pData);
            xQueueSend( xEventQueue, &event, 0UL );
            ucInputIndex = 0;
        }
	}


	vTaskDelete( xPrvExtUartRxTask );
}

static void blockExtUartTxTask( void *pvParameters )
{
    QueueHandle_t xEventQueue = (QueueHandle_t)pvParameters;
	UartTxString tmpData;
	for( ;; )
	{
		/* Block to wait for data arriving on the queue. */
		xQueueReceive(xExtUartTxBlockQueue, &tmpData, portMAX_DELAY );
		if(tmpData.string_length != 0){
			vExtUartTx(tmpData.pData,tmpData.string_length);
		}
	}
	vTaskDelete( xBlockExtUartTxTask );
}

// 接收来自天线的串口数据， 怎么组帧？ 组帧完成发到EVENT_EXT_UART_RX_OK事件处理器由DMA发送出去
// 发送成功的事件 EVENT_EXT_UART_TX_OK 通知事件处理器
int createPrvExtUartRxTask(QueueHandle_t xEventQueue){

    xComPortHandle xPort;
	/* Initialise the UART. */
	xPort = initExtUart(1000, xEventQueue);

    BaseType_t xReturn = xTaskCreate( prvExtUartRxTask, 
	                                  (const char *) "prvExtUartRxTask", 
									  configMINIMAL_STACK_SIZE * 2, 
									  xEventQueue, 
									  tskIDLE_PRIORITY + 3, /* gw_priority */
									  &xPrvExtUartRxTask);
	if(xReturn != pdPASS){
		return -1;
	}
	return 0;
}

int createBlockExtUartTxTask(QueueHandle_t xEventQueue){

	xExtUartTxBlockQueue = xQueueCreate(EXT_UART_TX_MSG_LENGTH, sizeof(UartTxString) );
	configASSERT( xExtUartTxBlockQueue );

    BaseType_t xReturn = xTaskCreate( blockExtUartTxTask, 
	                                  (const char *) "blockExtUartTxTask", 
									  configMINIMAL_STACK_SIZE * 2, 
									  xEventQueue, 
									  tskIDLE_PRIORITY + 4, /* gw_priority */
									  &xBlockExtUartTxTask);
	if(xReturn != pdPASS){
		return -1;
	}
	return 0;
}

void pushExtUartTxQueue(u8* pcMessage , u16 usStringLength){
	UartTxString tmpData;
	u8 *msgPtr = (u8*)pvPortMalloc(sizeof(u8)*usStringLength); // 指针在 EVENT_EXT_UART_TX_OK 中释放
	memcpy(msgPtr,pcMessage,usStringLength);
	tmpData.string_length = usStringLength;
	tmpData.pData = (char*)msgPtr;
    xQueueSend( xExtUartTxBlockQueue, &tmpData, 0UL );
}
